<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    .shenren{display: none}
</style>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>编辑公文分发</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>分发日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="dt" id="dt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>分发人</label>
                        <div class="layui-input-block">
                            <input type="text" name="baner" id="baner" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>分发类型</label>
                        <div class="layui-input-block">
                            <select id="fentype" name="fentype" lay-filter="fentype"></select>
                        </div>
                    </div>
                    <div class="layui-col-md6" id="wen_fa">
                        <label class="layui-form-label"><text>*</text>关联发文</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="faid" class="layui-input faid" onclick="openPage('../public/choose_wen_fa.php?dom=faid','80%','80%')">
                            <div class="choose_div" id="faid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_wen_fa.php?dom=faid','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-hide" id="wen_shou">
                        <label class="layui-form-label"><text>*</text>关联收文</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="shouid" class="layui-input shouid" onclick="openPage('../public/choose_wen_shou.php?dom=shouid','80%','80%')">
                            <div class="choose_div" id="shouid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_wen_shou.php?dom=shouid','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>分发对象</label>
                        <div class="layui-input-block" id="fenduiDiv"></div>
                    </div>
                    <div class="layui-col-md6" id="duiDept">
                        <label class="layui-form-label"><text>*</text>部门</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=deptid&type=dept&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="deptid"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=deptid&type=dept&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="deptid_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-hide" id="duiUser">
                        <label class="layui-form-label"><text>*</text>用户</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=userid&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="userid"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=userid&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="userid_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">终止查阅日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="zhongdt" id="zhongdt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>


    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;

    $("#commentForm").validate({
        rules: {
            dt: {required: true},
            baner: {required: true},
        },
        messages:{
            dt: {required: "请选择"},
            baner: {required: "请输入"},
        }
    });


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_wen_fen_edit, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                var weninfo = res.data.weninfo;

                $("#dt").val(data.dt);
                $("#baner").val(data.baner);
                $("#zhongdt").val(data.zhongdt);
                $("#explain").val(data.explain);


                var fentype_str = '';
                var fentype = res.data.fentype;
                for (var key in fentype){
                    if(data.fentype == key){
                        fentype_str += '<option value="'+key+'" selected>'+fentype[key]+'</option>';
                    }else{
                        fentype_str += '<option value="'+key+'">'+fentype[key]+'</option>';
                    }
                }
                $("#fentype").html(fentype_str);


                var fendui_str = '';
                if(data.fendui == 'dept'){
                    fendui_str += '<input type="radio" name="fendui" value="dept" checked title="部门" lay-filter="fendui">';
                    fendui_str += '<input type="radio" name="fendui" value="user" title="用户" lay-filter="fendui">';
                }else{
                    fendui_str += '<input type="radio" name="fendui" value="dept" title="部门" lay-filter="fendui">';
                    fendui_str += '<input type="radio" name="fendui" value="user" checked title="用户" lay-filter="fendui">';
                }

                $('#fenduiDiv').html(fendui_str);



                if(data.fentype == 1){
                    set_choose_btn('faid',weninfo.id,weninfo.title);
                    $("#wen_fa").removeClass('layui-hide');
                    $("#wen_shou").addClass('layui-hide');
                }else{
                    set_choose_btn('shouid',weninfo.id,weninfo.title);
                    $("#wen_fa").addClass('layui-hide');
                    $("#wen_shou").removeClass('layui-hide');
                }


                if(data.fendui == 'dept'){
                    tan_tags_init('#deptid', data.deptid_info);
                }else{
                    tan_tags_init('#userid', data.userid_info);
                }



                public_upload_file_load('upload_files', res.data.files)
                render_from();
                initParseMoney();
            }
        });

        form.on('select(fentype)',function (data) {
            if(data.value == 1){
                $("#wen_fa").removeClass('layui-hide');
                $("#wen_shou").addClass('layui-hide');
            }else{
                $("#wen_fa").addClass('layui-hide');
                $("#wen_shou").removeClass('layui-hide');
            }
        });

        form.on('radio(fendui)',function (data) {
            if(data.value == 'dept'){
                $("#duiDept").removeClass('layui-hide');
                $("#duiUser").addClass('layui-hide');
            }else{
                $("#duiDept").addClass('layui-hide');
                $("#duiUser").removeClass('layui-hide');
            }
        });

        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();

            if(data.field.fentype == 1){
                data.field.faid = get_choose_val('faid');
                formValid('faid');
                if(data.field.faid == undefined || !data.field.faid){
                    validRes = false;
                }
            }else{
                data.field.shouid = get_choose_val('shouid');
                formValid('shouid');
                if(data.field.shouid == undefined || !data.field.shouid){
                    validRes = false;
                }
            }


            if(data.field.fendui == 'dept'){
                data.field.fener = tan_tags_getvalue("#deptid", 'dept');
                if(!data.field.fener){
                    $("#deptid").parents('.tanxian').addClass('tanerror');
                    $('#deptid_tanerror').text('请选择');
                    validRes = false;
                }else{
                    $("#deptid").parents('.tanxian').removeClass('tanerror');
                    $('#deptid_tanerror').text('');
                }
            }else{
                data.field.fener = tan_tags_getvalue("#userid", 'user');
                if(!data.field.fener){
                    $("#userid").parents('.tanxian').addClass('tanerror');
                    $('#userid_tanerror').text('请选择');
                    validRes = false;
                }else{
                    $("#userid").parents('.tanxian').removeClass('tanerror');
                    $('#userid_tanerror').text('');
                }
            }

            if(!validRes){return false;}



            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            facade.ajax({url: ask_wen_fen_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });



</script>
</body>
</html>
